package com.vogue.supplymall.order.controller;

import com.vogue.supplymall.common.constant.ConstantBean;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.OrderDetailInfo;
import com.vogue.supplymall.common.domain.dto.OrderInfo;
import com.vogue.supplymall.common.domain.dto.OrderInfoCombination;
import com.vogue.supplymall.common.domain.dto.PaymentInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.tool.MessageSourceUtils;
import com.vogue.supplymall.common.exception.DataNotFindException;
import com.vogue.supplymall.order.common.CheckOrderUtils;
import com.vogue.supplymall.order.domain.biz.OrderPayBiz;
import com.vogue.supplymall.order.domain.biz.OrderUpdateBiz;
import com.vogue.supplymall.order.domain.service.OrderPaymentService;
import com.vogue.supplymall.order.exception.UpdateOrderException;
import com.vogue.supplymall.payment.dto.IResultParameter;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * The type Order update controller.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-02-07
 */
@RestController
public class OrderUpdateController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger log = LoggerFactory.getLogger(OrderUpdateController.class);

    @Resource
    private OrderUpdateBiz orderUpdateBiz;
    @Resource
    private OrderPaymentService orderPaymentService;
    @Resource
    private MessageSourceUtils messageSourceUtils;
    @Resource
    private OrderPayBiz orderPayBiz;

    /**
     * Order pay offline response entity.
     * 线下支付-买家
     *
     * @param appver          the appver
     * @param orderDetailInfo the order detail info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/pay/offline", method = RequestMethod.POST)
    public ResponseEntity<Object> orderPayOffline(@PathVariable("appver") String appver, @RequestBody OrderDetailInfo orderDetailInfo) {
        log.info("order pay offline start >>>>>> ");
        if (null == orderDetailInfo.getOrderid() || null == orderDetailInfo.getStatusid()) {
            throw new UpdateOrderException("");
        }
        OrderInfo orderinfo = new OrderInfo();
        orderinfo.setOid(String.valueOf(orderDetailInfo.getOrderid()));
        orderinfo.setPaytype(0);
        ControllerResultInfo resultInfo = new ControllerResultInfo();
//        orderUpdateBiz.orderPayOffline(orderDetailInfo);
        resultInfo.setData(orderPayBiz.orderPay(orderinfo));
        resultInfo.setResult(true);
        resultInfo.setCode(ConstantBean.SHAREGO_SUCCESS_CODE);
        resultInfo.setMsg(messageSourceUtils.getMessage("order.pay.success", null));
        log.info(" <<<<<< order pay offline end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Update status response entity.
     * 更新订单状态-买家
     *
     * @param appver          the appver
     * @param orderDetailInfo the order detail info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/update/status", method = RequestMethod.POST)
    public ResponseEntity<Object> updateStatus(@PathVariable("appver") String appver, @RequestBody OrderDetailInfo orderDetailInfo) {
        log.info("update order status start >>>>>> ");
        UserInfo info = new UserInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            log.info("获取登信息：" + info.getLoginId());
            if (CheckOrderUtils.checkUpdateStatusParameter(orderDetailInfo)) {
                log.info(" 参数错误！ ");
                throw new DataNotFindException("parameter error");
            }
        } else {
            log.info("获取登信息失败！");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        Boolean result = orderUpdateBiz.updateStatus(orderDetailInfo, info);
        if (result) {
            resultInfo.setResult(true);
            resultInfo.setMsg(messageSourceUtils.getMessage("2612", new String[]{}));
        } else {
            throw new UpdateOrderException("");
        }
        log.info(" <<<<<< update order status end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Update freight response entity.
     * 更新运费-卖家
     *
     * @param appver          the appver
     * @param orderDetailInfo the order detail info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/update/freight", method = RequestMethod.POST)
    public ResponseEntity<Object> updateFreight(@PathVariable("appver") String appver, @RequestBody OrderDetailInfo orderDetailInfo) {
        log.info("update order freight start >>>>>> ");
        UserInfo info = new UserInfo();
        if (SecurityUtils.getSubject().isAuthenticated()) {
            info = (UserInfo) SecurityUtils.getSubject().getPrincipal();
            log.info("获取登信息：" + info.getLoginId());
            if (CheckOrderUtils.checkUpdateFreightParameter(orderDetailInfo)) {
                log.info(" 参数错误！ ");
                throw new DataNotFindException("parameter error");
            }
        } else {
            log.info("获取登信息失败！");
        }

        Map<String, Object> data;
        try {
            data = orderUpdateBiz.updateFreight(orderDetailInfo, info);
        } catch (UpdateOrderException e) {
            throw new UpdateOrderException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(data);
        resultInfo.setMsg(messageSourceUtils.getMessage("2612", new String[]{}));
        log.info(" <<<<<< update order freight end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Order cancel response entity.
     * 取消订单
     *
     * @param appver          the appver
     * @param orderDetailInfo the order detail info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/cancel", method = RequestMethod.POST)
    public ResponseEntity<Object> orderCancel(@PathVariable("appver") String appver, @RequestBody OrderDetailInfo orderDetailInfo) {
        log.info("order cancel start >>>>>> ");
        if (null == orderDetailInfo.getOrderid()
                || null == orderDetailInfo.getStatusid()
                || null == orderDetailInfo.getCancelcause()
                || null == orderDetailInfo.getBuyerSellerType()) {
            throw new UpdateOrderException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        orderUpdateBiz.orderCancel(orderDetailInfo);
        resultInfo.setResult(true);
        resultInfo.setMsg(messageSourceUtils.getMessage("2612", new String[]{}));
        log.info(" <<<<<< order cancel end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Order delete response entity.
     * 删除订单
     *
     * @param appver          the appver
     * @param orderDetailInfo the order detail info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/delete", method = RequestMethod.POST)
    public ResponseEntity<Object> orderDelete(@PathVariable("appver") String appver, @RequestBody OrderDetailInfo orderDetailInfo) {
        log.info("order delete start >>>>>> ");
        if (null == orderDetailInfo.getOrderid() || null == orderDetailInfo.getStatusid()) {
            throw new UpdateOrderException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        orderUpdateBiz.orderDelete(orderDetailInfo);
        resultInfo.setResult(true);
        resultInfo.setMsg(messageSourceUtils.getMessage("2612", new String[]{}));
        log.info(" <<<<<< order delete end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Order agree cancel response entity.
     * 卖家处理是否同意买家取消订单的申请
     *
     * @param appver          the appver
     * @param orderDetailInfo the order detail info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/order/agreecancel", method = RequestMethod.POST)
    public ResponseEntity<Object> orderAgreeCancel(@PathVariable("appver") String appver, @RequestBody OrderDetailInfo orderDetailInfo) {
        log.info("order cancel start >>>>>> ");
        if (null == orderDetailInfo.getOrderid()
                || null == orderDetailInfo.getStatusid()
                || null == orderDetailInfo.getBuyerSellerType()) {
            throw new UpdateOrderException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        orderUpdateBiz.orderAgreeCancel(orderDetailInfo);
        resultInfo.setResult(true);
//        if(orderDetailInfo.getIsAgreeCancel()){
//            resultInfo.setMsg("同意买家取消订单！");
//        }else{
//            resultInfo.setMsg("拒绝买家取消订单！");
//        }
        log.info(" <<<<<< order cancel end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * 获取支付需要的信息(线上支付)
     *
     * @param appver    the appver
     * @param orderInfo the order info
     * @return order info for pay
     */
    @RequestMapping(value = "/is/{appver}/order/pay/online", method = RequestMethod.POST)
    public ResponseEntity<Object> getOrderInfoForPay(@PathVariable("appver") String appver, @RequestBody OrderInfo orderInfo) {
    	IResultParameter rp = orderPayBiz.orderPay(orderInfo);
//    	if (orderInfo.getPaytype() != null
//    			&& orderInfo.getPaytype().intValue() == ConstantBean.EnumPaymentMode.AdvancePayment.getCode()) {
//    		orderUpdateBiz.payAdvance(orderInfo);
//    		rp = new ResultParameter();
//    	} else {
//    		if (orderInfo.getPaytype() != null &&  orderInfo.getPaytypeid() == null) {
//    			if (orderInfo.getPaytype().intValue() == ConstantBean.EnumPaymentMode.Alipay.getCode()) {
//        			orderInfo.setPaytypeid(8);
//        		} else if (orderInfo.getPaytype().intValue() == ConstantBean.EnumPaymentMode.WeChat.getCode()) {
//        			orderInfo.setPaytypeid(6);
//        		} else if (orderInfo.getPaytype().intValue() == ConstantBean.EnumPaymentMode.WuXiang.getCode()) {
//        			orderInfo.setPaytypeid(20);
//        		}
//    		}
//    		rp = orderUpdateBiz.payOnline(orderInfo);
//    	}
    	
		ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setCode(ConstantBean.SHAREGO_SUCCESS_CODE);
        resultInfo.setMsg(messageSourceUtils.getMessage("order.pay.success", null));
        resultInfo.setData(rp.getData());
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * 组合方式付款
     *
     * @param appver               the appver
     * @param orderInfoCombination the order info combination
     * @return response entity
     */
    @RequestMapping(value = "/is/{appver}/order/pay/combination", method = RequestMethod.POST)
    public ResponseEntity<Object> orderCombinationPay(@PathVariable("appver") String appver, @RequestBody OrderInfoCombination orderInfoCombination) {
    	for (PaymentInfo paymentInfo : orderInfoCombination.getOrderInfo().getPaymentList()) {
    		log.info("订单id为{}的组合支付方式包含：{}", orderInfoCombination.getOrderInfo().getOid(), paymentInfo.getPaymentMode());
    	}
    	IResultParameter rp = orderUpdateBiz.payCombinationPayment(orderInfoCombination.getOrderInfo(), orderInfoCombination.getOrderDetailInfo());
		ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setCode(ConstantBean.SHAREGO_SUCCESS_CODE);
        resultInfo.setMsg(messageSourceUtils.getMessage("order.pay.success", null));
        if (rp != null) {
        	resultInfo.setData(rp.getData());
        } else {
            resultInfo.setData(null);
        }
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
